package cn.tradewin.pmp.pages.master;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.Link;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.BeanModelSource;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;

import cn.tradewin.pmp.persist.model.MEmp;
import cn.tradewin.pmp.services.EmpManageService;

public class EmpManage {
	
	@Property
	private final EmpEncoder empEncoder = new EmpEncoder();

	@Property
	List<MEmp> emps;

	private MEmp emp;
	
	@Property
	private MEmp updEmp;

	@Property
	private MEmp addEmp;

	private boolean inFormSubmission;

	private List<MEmp> empsSubmitted;

	@Inject
	private EmpManageService empService;

	@Inject
	private BeanModelSource beanModelSource;
	
	@Property
	private BeanModel<MEmp> beanModel;

	@Inject
	private Messages messages;
	
	@Inject
	private ComponentResources resources;
	
	@Environmental
	private JavaScriptSupport js;
	
	@Inject
	Logger log;
	
	@Log
	void onActivate() {
		inFormSubmission = false;
	}

	@Log
	void setupRender() {
		beanModel = beanModelSource.createDisplayModel(MEmp.class, messages);
		beanModel.add("update", null);
		beanModel.include("selected", "empName","email", "tel01","tel02","chat","gendle","birthday","hireDate","update");
		beanModel.get("selected").label("");
		beanModel.get("selected").sortable(false);
		beanModel.get("empName").sortable(false);
//		beanModel.get("empDesc").sortable(false);
		beanModel.get("update").label("");
		beanModel.get("update").sortable(false);
	}

	@Log
	void onPrepareForRender() {
		if (emps == null) {
			emps = empService.getAllEmps();
		}
	}

	@Log
	void onPrepare() {
		if (addEmp == null) {
			addEmp = new MEmp();
		}
		if (updEmp == null) {
			updEmp = new MEmp();
		}
	}

	@Log
	void afterRender() {
		js.require("jquery");
		js.require("bootstrap");
		JSONObject params = new JSONObject();
		params.put("id", "empList");
		params.put("url", this.getPageLink());
		js.require("EmpManage").invoke("init").with(params);
	}

	@Log
	void onPrepareForSubmit() {
		inFormSubmission = true;
		empsSubmitted = new ArrayList<MEmp>();
	}

	/**
	 * delete selected department
	 */
	@Log
	void onSuccessFromListForm() {
//		List<Integer> delEmps = new ArrayList<Integer>();
//		for (MEmp m : empsSubmitted) {
//			if (m.isSelected()) {
//				delEmps.add(m.getEmpNo());
//			}
//		}
//		if (delEmps.size() > 0) {
//			empService.DelEmps(delEmps);
//		}
	}
	
//	void onSuccessFromUpdForm() {
//		try {
//			updEmp.setUpdateDatetime(new Date());
//			empService.updEmp(updEmp);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

	@Log
	void onSuccessFromAddForm() {
		try {
			empService.AddEmp(addEmp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Log
	JSONObject onUpdateEmp(String empNo) {
		ObjectMapper objectMapper = new ObjectMapper();
		updEmp = empService.findEmpByPrimary(empNo);
		if (updEmp != null) {
			try {
				return new JSONObject(objectMapper.writeValueAsString(updEmp));
			} catch (JsonGenerationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonMappingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

	
	private String getPageLink() {
		Link link = resources.createEventLink("updateEmp");
		return link.toAbsoluteURI();
	}
	
	public boolean isNoData() {
		if (emps == null || emps.size() == 0)
			return true;
		return false;
	}
	
	public MEmp getEmp() {
		return emp;
	}

	public void setEmp(MEmp emp) {
		this.emp = emp;
		if (inFormSubmission) {
			this.empsSubmitted.add(emp);
		}
	}
	
	private class EmpEncoder implements ValueEncoder<MEmp> {

		public String toClient(MEmp value) {
			return value.getEmpNo().toString();
		}

		public MEmp toValue(String clientValue) {
			MEmp m = new MEmp();
			m.setEmpNo(clientValue);
			return m;
		}
	}
}
